<template>
  <div class="flexContent">
    <div class="searchBox">

      <el-form class="demo-form-inline" label-position="left" label-width="auto">
        <div class="serFlexBox">
          <div class="inputBox">
            <el-row :gutter="12" style="display: flex;flex-wrap: wrap;">
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="合同">
                  <el-input v-clearEmoji v-model.trim="seaData.name" style="width: 95%" clearable placeholder="编号/名称"
                    @keyup.enter.native="serchFn" />
                </el-form-item>
              </el-col>
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="商品税率">
                  <el-select @click.native="getInputRate" v-model="seaData.commodityRate" clearable popper-class="eloption"
                    style="width: 95%">
                    <el-option v-for="(val, key) in commodityRateList" :key="key" :value="key" :label="val" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="供应商名称">
                  <el-select @click.native="getSCMList" v-model.trim="seaData.supplierId" clearable filterable
                    popper-class="eloption" style="width: 95%">
                    <el-option v-for="firstSupplier in firstSupplierList" :key="firstSupplier.id"
                      :value="firstSupplier.id" :label="firstSupplier.supplierNum + '   ' + firstSupplier.name
                        " />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :xs="12" :sm="12" :md="12" :lg="10" :xl="6">
                <el-form-item label="更新日期">
                  <el-date-picker v-model="date" style="width: 95%" type="daterange" unlink-panels range-separator=""
                    start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd" />
                </el-form-item>
              </el-col>
            </el-row>
          </div>
          <div class="btnBox">
            <el-button @click.stop="serchFn" class="searchBtn" icon="el-icon-search">搜索</el-button>
            <el-button @click.stop="resetFn" class="resetBtn" icon="el-icon-refresh-right">重置</el-button>
          </div>
        </div>
      </el-form>
    </div>
    <div class="add">
      <el-button class="addBtn" v-has="'contract_add'" @click="addFn">新增</el-button>
    </div>
    <div class="table" id="tableBox" v-height>
      <div style="flex:1;">
        <vxe-table :column-config="{resizable: true}" border class="tablebox" ref="xTable1" :row-config="{isHover: true}" :scroll-y="{ enabled: true,gt:21 }" style="width: 100%" height="100%" size="medium" align="center">
          <vxe-column type="seq" width="80" title="序号" />
          <vxe-column field="number" title="合同编号" :min-width="elTableAdaptationWidth('number', contractList)"/>
          <vxe-column field="name" title="合同名称" :min-width="elTableAdaptationWidth('name', contractList)"/>
          <vxe-column field="supplierName" title="供应商名称" :min-width="elTableAdaptationWidth('supplierName', contractList)"/>
          <vxe-column field="commodityRate" title="商品税率" min-width="100"/>
          <vxe-column field="salespersonName" title="业务员姓名" min-width="150"/>
          <vxe-column field="updateTime" title="更新日期" min-width="150">
            <template v-slot="{ row }">
              {{ row.updateTime | parseTime }}
            </template>
          </vxe-column>
          <vxe-column v-if="$showFn(['contract_detail', 'contract_edit', 'contract_del'])"
            fixed="right" width="150" title="操作">
            <template v-slot="{ row }">
              <el-button type="text" style="margin-right: 10px; color: rgb(0, 132, 255)" v-has="'contract_detail'"
                @click.stop="getDetail(row)">详情</el-button>
              <el-button slot="reference" style="color: rgb(0, 132, 255)" type="text" @click.stop="edit(row)"
                v-has="'contract_edit'">修改</el-button>
              <el-button type="text" style="margin-right: 10px; color: red" @click="$delFn('合同', del, row)"
                v-has="'contract_del'">删除</el-button>
            </template>
          </vxe-column>
        </vxe-table>
      </div>
      <div class="paginationBox">
        <el-pagination :current-page="page.pageNum" background :page-sizes="$pageSizes" :page-size="page.pageSize"
          layout="total,->, sizes, prev, pager, next, jumper" :total="page.total" @size-change="handleSizeChange"
          @current-change="handleCurrentChange" />
      </div>
    </div>
  </div>
</template>

<script>
import { getContractList, delContract } from "@/api/supplier";
import { getSCMList } from "@/api/scm";
import { getDictValueAndNameSource } from "@/api/dictionary";
export default {
  name: "ContractManagement",
  data() {
    return {
      width: "50%",
      title: "新增",
      showDialog: false,
      shopList: [],
      name: "",
      sta: "",
      isSearch: false,
      firstSupplierList: [],
      seaData: {
        commodityRate: "",
        maxDate: "",
        minDate: "",
        name: "",
        supplierId: "",
      },
      date: [],
      commodityRateList: [],
      page: {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      },
      contractList: [],
      posId: "",
    };
  },
  created() {
    this.getContractList();
    // this.getSCMList();
    // this.getInputRate();
  },
  methods: {
    async getContractList() {
      if (!this.isSearch) {
        this.seaData = {
          commodityRate: "",
          maxDate: "",
          minDate: "",
          name: "",
          supplierId: "",
        };
      }
      const res = await getContractList({ ...this.page, ...this.seaData });
      if (res.code === 200) {
        console.log(res);
        const result = res.data;
        this.contractList = result.list;
        this.page.pageSize = result.pageSize;
        this.page.pageNum = result.pageNum;
        this.page.total = result.total;
        const $table = this.$refs.xTable1
        $table.loadData(this.contractList);
        console.log("contractList", this.contractList);
      } else {
        // this.$message.error(res.message);
      }
    },
    async getInputRate() {
      const res = await getDictValueAndNameSource('TAX_RATE');
      if (res.code === 200) {
        this.commodityRateList = res.data;
        // console.log(this.levelList, '级别')
      }
    },
    async getSCMList() {
      const res = await getSCMList();
      if (res.code === 200) {
        const data = res.data;
        this.firstSupplierList = data.list;
        // console.log(data)
        // console.log('供应商', this.firstSupplierList)
      }
    },
    async serchFn() {
      this.page = {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      };
      this.isSearch = true;
      this.seaData.maxDate = this.date?.length ? this.date[1] : "";
      this.seaData.minDate = this.date?.length ? this.date[0] : "";
      this.getContractList();
    },
    resetFn() {
      this.page = {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      };
      this.isSearch = false;
      this.date = [];
      this.seaData = {
        commodityRate: "",
        maxDate: "",
        minDate: "",
        name: "",
        supplierId: "",
      };
      this.getContractList();
    },
    addFn() {
      this.$router.push("contractAdd");
    },
    // 获取条数改变
    handleSizeChange(val) {
      this.page.pageSize = val;
      this.page.pageNum = 1;
      console.log(`每页 ${val} 条`);
      this.getContractList();
    },
    // 获取页数改变
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.page.pageNum = val;
      this.getContractList();
    },
    async getDetail(row) {
      this.$router.push({
        path: "contractDetail",
        query: {
          type: "detail",
          id: row.id,
        },
      });
    },
    async edit(row) {
      this.$router.push({
        path: "contractEdit",
        query: {
          type: "edit",
          id: row.id,
        },
      });
    },
    async del(row) {
      // console.log(row)
      const res = await delContract({ id: row.id });
      if (res.code === 200) {
        this.$message.success(res.message);
        if (this.contractList.length === 1 && this.page.pageNum > 1)
          this.page.pageNum--;
        this.getContractList();
      } else {
        // this.$message.error(res.message);
      }
      // console.log(res)
    },
  },
};
</script>

<style lang="scss" scoped></style>
